//
//  ValidTermViewController.m
//  tuanGG
//
//  Created by 李洪旭 on 15/11/19.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "ValidTermViewController.h"
#import "LimitTableViewCell.h"
#import "LqkinDataPicker.h"
@interface ValidTermViewController ()<UITableViewDelegate,UITableViewDataSource,LqkinDataPickerDelegate>
{
    UITableView *tableView;
}
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,assign)NSInteger index;


@property (nonatomic,assign)NSInteger oldindex;
@property (nonatomic,strong)NSIndexPath * oldindexpath;
@property (nonatomic,strong)NSString * startDate;
@property (nonatomic,strong)NSString * endDate;
@property (nonatomic,assign)NSInteger number;
@property (nonatomic,strong)NSArray *imageArray;

@property (nonatomic,strong)NSMutableArray * flagArray;
@end

@implementation ValidTermViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _flagArray = [[NSMutableArray alloc]init];
    [self addnavigationTitle:@"有效期限" navColor:@"4fd2be"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0efed"];
    [self initlizeDataSource];
    [self addTableView];
}
- (void)initlizeDataSource {
    _imageArray = @[@"ic_unchecked",@"ic_checked"];
    _dataArray = @[@[@"开始时间",@"结束时间"],@[@"周末，法定节假日通用",@"除周末和法定节假日外通用"]];
    if (![[User_member instance].availabletime isEqualToString:@""]) {
        NSArray *array = [[User_member instance].availabletime componentsSeparatedByString:@"-"];
        NSLog(@"%@",array);
        _startDate = array[0];
        NSString *string = array[1];
        NSArray *array1 = [string componentsSeparatedByString:@"a"];
        NSLog(@"%@",array1);
        _endDate = array1[0];
        _number = [array1[1]integerValue];

        for (int i = 0; i < 2; i ++) {
            if (_number==i+1) {

                [_flagArray addObject:@(1)];
            }
            else
            {
                [_flagArray addObject:@(0)];
            }
        }

        [tableView reloadData];
    }
    else
    {
        for (int i = 0; i < 2; i ++) {

                [_flagArray addObject:@(0)];

        }

    }

}
- (void)addTableView {
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 350) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    tableView.tableFooterView = [[UITableView alloc]init];
    [tableView registerClass:[LimitTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:tableView];
    
    UIView *footButton = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    
    UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 25, ScreenWidth-30, 45)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.layer.cornerRadius = 5;
    saveButton.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [saveButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    [footButton addSubview:saveButton];
    tableView.tableFooterView = footButton;
}
#pragma mark - 事件实现
- (void)saveButton:(UIButton *)sender{
    NSLog(@"保存信息");
    LimitTableViewCell * cell = (LimitTableViewCell *)[tableView cellForRowAtIndexPath:_oldindexpath];
    if ([cell.statsLabel.text isEqualToString:@""] || [cell.stopLabel.text isEqualToString:@""]) {
        [Common addAlert:@"请选择开始或者结束时间"];
    }else if (_index == 0) {
        [Common addAlert:@"请选择适用时间"];
    }else {
        [self NetWorking];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray[section]count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LimitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0 ) {
        cell.seledImage.hidden = YES;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        cell.userInteractionEnabled = NO;
        if (indexPath.row == 0) {
            cell.statsLabel.text  = _startDate;
        }else if (indexPath.row == 1){
            cell.stopLabel.text = _endDate;
        }

    }else {
        cell.seledImage.hidden = NO;
        BOOL isopen = [_flagArray[indexPath.row] boolValue];
        if (isopen) {
             [cell.seledImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_checked")];
        }
        else
        {
             [cell.seledImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_unchecked")];
        }


    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row+1);
    _index = indexPath.row+1;
    if (indexPath.section == 0) {

        _oldindex = indexPath.row;
        _oldindexpath = indexPath;
        switch (indexPath.row) {
            case 0:
            {
                LqkinDataPicker * picker = [[LqkinDataPicker alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, ScreenHeight)type:1];
                picker.delegate = self;
               
                [self.view addSubview:picker];
            }
                break;
            case 1:
            {
                LqkinDataPicker * picker = [[LqkinDataPicker alloc]initWithFrame:CGRectMake(0, -64, ScreenWidth, ScreenHeight)type:1];
                picker.delegate = self;

                [self.view addSubview:picker];
            }
                break;
            default:
                break;
        }
    }
    else
    {

       for (int i = 0; i < _flagArray.count; i ++) {
           if(i == indexPath.row){
                _flagArray[i]=@1;
           }else{

               _flagArray[i]=@0;
           }
       }

//        for (int i = 0; i < _flagArray.count; i ++) {
//
//
//
//
//
//            if ([_flagArray[i] boolValue]&& i == indexPath.row) {
//                    BOOL isopen = [_flagArray[i] boolValue];
//                    isopen = !isopen;
//                    [_flagArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isopen]];
//                }
//            else
//            {
//                BOOL isopen = [_flagArray[i] boolValue];
//                isopen = !isopen;
//                [_flagArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isopen]];
//            }
//
//        }

        [tableView reloadData];
    }
    
}
- (void)LqkinPikerViewDelegateDateyear:(NSString *)year dmonth:(NSString *)month dday:(NSString *)day hour:(NSString *)hour minute:(NSString *)minute timestamp:(NSString *)timestamp
{
     LimitTableViewCell * cell = (LimitTableViewCell *)[tableView cellForRowAtIndexPath:_oldindexpath];
    if (_oldindex == 0) {
        //

        _startDate = [NSString stringWithFormat:@"%@.%@.%@",year,month,day];

        cell.statsLabel.text = _startDate;
    }
    else if (_oldindex == 1)
    {
        _endDate = [NSString stringWithFormat:@"%@.%@.%@",year,month,day];
        cell.stopLabel.text = _endDate;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self NetWorking];
    }
}
- (void)NetWorking {
    NSString *stirng = [NSString stringWithFormat:@"%@-%@a%ld",_startDate,_endDate,_index];
    NSLog(@"%@",stirng);
    NSDictionary *dictionary = @{@"content":stirng};
    [GLAFRequest startRequest:availabletime_url paramsDic:dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue ] == 1) {
            [Common addAlert:@"保存成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {
        
    }];
}
@end
